<?php
namespace Api\Controller;

use Api\Controller\BaseController;
use Lib\WXMicro\WXSession;
use Lib\WXMicro\WXBizDataCrypt;
use Think\Log;
use Lib\WXMicro\ErrorCode;
use Think\Crypt;

/**
 * 用户操作
 *
 * @author ryan
 *        
 */
class UserController extends BaseController
{

    public function weixinlogin()
    {
        $code = I('code');
        $encryptedData = I('encryptedData');
        $iv = I('iv');
        $appid = C('MICRO_APPID');
        $appscrect = C('MICRO_APPSECRET');
        if (empty($code)) {
            $this->result(1000, array(), 'js_code为空');
        }
        $session = new WXSession($appid, $appscrect);
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$appscrect}&js_code={$code}&grant_type=authorization_code";
        $result = $session->getSessionKey($url);
        Log::record($url);
        Log::record(serialize($result));
        if (empty($result) || ! isset($result['session_key'])) {
            $this->result(1001, array(), 'session_key获取失败');
        }
        if (! isset($result['openid'])) {
            $this->result(1002, array(), '授权失败');
        }
        $data = array();
        $wxbizData = new WXBizDataCrypt($appid, $result['session_key']);
        $errcode = $wxbizData->decryptData(urldecode($encryptedData), urldecode($iv), $data);
        Log::record(serialize($errcode));
        Log::record(serialize($data));
        if ($errcode === ErrorCode::$OK) {
            $open_id = $result['openid'];
            if (isset($result['unionid'])) {
                $unionid = $result['unionid'];
            }
            $member = D('Member')->where(array(
                'wx_openid' => $open_id
            ))->find();
            if (empty($member)) {
                $info = array();
                $info['nick_name'] = $data['nickName'];
                $info['wx_openid'] = $open_id;
                $info['wx_unionid'] = empty($unionid) ? '' : $unionid;
                $info['wx_info'] = Crypt::encrypt($data, $open_id);
                $info['sex'] = $data['gender'];
                $info['add_time'] = TIMESTAMP;
                $member_id = D('Member')->add($info);
                if ($member_id === false) {
                    $this->result(1003, array(), '登录失败[添加用户失败]');
                }
            } else {
                $member_id = $member['id'];
            }
            $token = array();
            $token['member_id'] = $member_id;
            $token['add_time'] = TIMESTAMP;
            $token['status'] = 0;
            $token['token'] = md5($token['member_id'] . $token['add_time']);
            $status = D('MemberToken')->add($token);
            if ($status === false) {
                $this->result(1004, array(), '登录失败[授权失败]');
            } else {
                $this->result(0, array(
                    'token' => $token['token']
                ), '登录成功');
            }
        } else {
            $this->result(1005, array(), '登录失败[解析失败]');
        }
    }

    /**
     * 需求列表
     */
    public function requestList()
    {
        $token = I('token');
        $page = I('page');
        $limit = I('limit');
        if (empty($token)) {
            $this->result(2000, array(), '无权限访问');
        }
        $member = D('MemberToken')->where(array(
            'token' => $token
        ))->find();
        if (empty($member)) {
            $this->result(2001, array(), '无权限访问');
        }
        $condition = array();
        $condition['member_id'] = $member['member_id'];
        $demands = D('Demand')->where($condition)
            ->limit($page, $limit)
            ->select();
        $count = D('Demand')->where($condition)->count();
        $pages = ceil($count / $limit);
        $data = array();
        $data['list'] = $demands;
        $data['pages'] = $pages;
        $data['cur_page'] = $page;
        $this->result(0, $data, '查询成功');
    }
}

